package com.yc.cloud.common.swagger.config;

import cn.hutool.core.util.ArrayUtil;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Swagger基础配置
 * 从SpringFox迁移到SpringDoc OpenAPI 3
 */
public abstract class BaseSwaggerConfig {

    @Bean
    public OpenAPI createOpenAPI() {
        SwaggerProperties swaggerProperties = swaggerProperties();
        
        // 创建API基本信息
        OpenAPI openAPI = new OpenAPI()
                .info(new Info()
                        .title(swaggerProperties.getTitle())
                        .description(swaggerProperties.getDescription())
                        .version(swaggerProperties.getVersion())
                        .contact(new Contact()
                                .name(swaggerProperties.getContactName())
                                .url(swaggerProperties.getContactUrl())
                                .email(swaggerProperties.getContactEmail())));
        
        // 添加安全配置
        if (swaggerProperties.isEnableSecurity()) {
            openAPI.components(new Components()
                    .addSecuritySchemes("Authorization", new SecurityScheme()
                            .type(SecurityScheme.Type.HTTP)
                            .scheme("bearer")
                            .bearerFormat("JWT")
                            .in(SecurityScheme.In.HEADER)
                            .name("Authorization")));
            
            openAPI.addSecurityItem(new SecurityRequirement().addList("Authorization"));
        }
        
        return openAPI;
    }
    
    @Bean
    public GroupedOpenApi createRestApi() {
        SwaggerProperties swaggerProperties = swaggerProperties();
        
        // 处理多个包名
        String[] basePackages = swaggerProperties.getApiBasePackage().split(",");
        for (int i = 0; i < basePackages.length; i++) {
            basePackages[i] = basePackages[i].trim();
        }
        
        // 构建API分组
        GroupedOpenApi.Builder builder = GroupedOpenApi.builder()
                .group("default")
                .packagesToScan(basePackages);
        
        // 添加排除路径
        if (ArrayUtil.isNotEmpty(swaggerProperties.getExcludePath())) {
            List<String> excludePaths = new ArrayList<>();
            for (String path : swaggerProperties.getExcludePath()) {
                excludePaths.add(path);
            }
            builder.pathsToExclude(excludePaths.toArray(new String[0]));
        }
        
        return builder.build();
    }
    
    /**
     * 自定义Swagger配置
     */
    public abstract SwaggerProperties swaggerProperties();
}